<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登陆页面</title>
    <link rel="stylesheet" href="css/login.css">
    <link rel="stylesheet" href="./css/common_.css">

    <script src="./js/jquery.min.js"></script>
</head>

<body>
<!-- 导航栏 -->
<div class="nav">
    <img src="./img/blog_logo.png" alt="">
    <p>思维驿站--燃笔博客</p>

    <a href="./blog_list.html" id="blog_outline">燃笔主页</a>

</div>
<div class="container">
    <p>登录</p>
    <div class="login"><span>用户名</span><input type="text" id="username" placeholder="请输入用户名" value="admin"></div>
    <div class="login"><span>密码&nbsp;&nbsp;&nbsp;&nbsp;</span>
        <input type="password" id="password" placeholder="请输入密码" value="admin">
    </div>
    <div class="login"><span>验证码</span>
        <input type="text" id="vr_code_pre" placeholder="请输入验证码">
        <img src="user/captcha" alt="验证码" id="vr_code" onclick="changeCode()">
    </div>


    <div class="btn">
        <button id="sub" type="submit" onclick="login()">登录</button>
        <button id="reg" type="submit" onclick="register()">注册</button>
    </div>
    <div id="for_pass">
        <a href="./for_pass.html">忘记密码?</a>
    </div>
</div>
<script>


    //面试官你好，为了更好的预览我的项目，在这里我将登录将需要填写的账号和密码已经自动填充上了，您只需要填写验证码后点击登录即可
    function login() {
        var $username = jQuery('#username')
        var $password = jQuery('#password')
        var $vrCode = jQuery('#vr_code_pre')
        if ($username.val() == '') {
            alert('请输入用户名！')
            $username.focus()
            return
        }
        if ($password.val() == '') {
            alert('请输入密码！')
            $password.focus()
            return
        }
        if ($vrCode.val() == '') {
            alert('请输入验证码！')
            $vrCode.focus()
            return
        }
        jQuery.ajax({
            url: "user/login",
            type: "post",
            data: {
                "username": $username.val(),
                "password": $password.val(),
                "captcha": $vrCode.val()
            },
            success: function (result) {
                if (result.code == 200) {
                    alert('登陆成功！')
                    location.href = "./myblog_list.html"
                } else {
                    alert(result.msg)
                }
                changeCode()    //更新验证码
            }
        })
    }
    function changeCode() {
        jQuery('#vr_code').attr('src', 'user/captcha?' + Math.random())
    }

    function register() {
        location.href = "reg.html"
    }

    $(document).keypress(function(e) {
        if((e.keyCode || e.which)==13) {
            login()
        }
    })

    changeCode()

</script>
</body>

</html>